<?php


namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 示例接口  loan
 */
class Daikuan extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [''];

    /**
     * 计算等额本息每月还款金额 和总利息
     *  money : 金额
     *  month : 月数
     *  rate : 年利率
     */
    public function index(){

        $money =$this->request->param('money');
        $month_t=$this->request->param('month');

        $month=getmonth($month_t);

        $rate =0.06/12;

        $monthly_repayment = $money * $rate * pow(1 + $rate, $month) / (pow(1 + $rate, $month) - 1);

        $total_interest = $monthly_repayment * $month - $money;

        $this->success('每月还款金额和总利息'.$month, ['monthly' => round($monthly_repayment,2), 'total' => round($total_interest,2)]);

    }

    /**
     * 提交信息
     * money:money,
     * name:name,
     * mobile:mobile,
     * total:total,
     * arrayt_t:arrayt_t,
     * monthly:monthly,
     */
    public function submits(){

        $name=$this->request->param('name');
        $mobile=$this->request->param('mobile');
        $money=$this->request->param('money');  //贷款金额
        $total=$this->request->param('total');  //利息
        $arrayt_t=$this->request->param('arrayt_t');  //期数
        $monthly=$this->request->param('monthly');  //每月
        $uid=$this->request->param('uid');




//        $log=Db::name('daikuan')
//            ->where(['mobile'=>$mobile,'money'=>$money,'arrayt_t'=>$arrayt_t])
//            ->find();
//
//
//        if($log){
//            $this->success('您已提交过了', $log['id']);
//        }

        $data=[
            'name'=>$name,
            'mobile'=>$mobile,
            'money'=>$money,
            'total'=>$total,
            'arrayt_t'=>$arrayt_t,
            'monthly'=>$monthly,
            'time'=>time(),
            'uid'=>$uid
        ];

        $res=Db::name('daikuan')
            ->insertGetId($data);

        if($res){
            $this->success('提交成功,请补全信息！', $res);
        }else{
            $this->error('提交失败');
        }



    }

    /**
      * 补充信息
     * id:id,
     * simg:this.simg,
     * wimg:this.wimg,
     * cptype:this.cptype,
     * cpname:this.cpname,
     * cpphone:this.cpphone,
     * cptime:this.cptime,
     * cpwork:this.cpwork,
     * sosname:this.sosname,
     * sosmobile:this.sosmobile
     * sosname1: this.sosname1,
     * sosmobile1: this.sosmobile1,
     * sosname2: this.sosname2,
     * sosmobile2: this.sosmobile2,
     * sosname3: this.sosname3,
     * sosmobile3: this.sosmobile3,
     * bank: this.bank,
     * address: this.address,
    */
    public function setdaikuan(){
        $id=$this->request->param('id');
        $simg=$this->request->param('simg');
        $wimg=$this->request->param('wimg');
        $cptype=$this->request->param('cptype');
        $cpname=$this->request->param('cpname');
        $cpphone=$this->request->param('cpphone');
        $cptime=$this->request->param('cptime');
        $cpwork=$this->request->param('cpwork');
        $sosname=$this->request->param('sosname');
        $sosmobile=$this->request->param('sosmobile');
        $cpadd=$this->request->param('cpadd');

        $sosname1=$this->request->param('sosname1');
        $sosmobile1=$this->request->param('sosmobile1');
        $sosname2=$this->request->param('sosname2');
        $sosmobile2=$this->request->param('sosmobile2');
        $sosname3=$this->request->param('sosname3');
        $sosmobile3=$this->request->param('sosmobile3');



        $data=[
            'simage'=>$simg,
            'wimage'=>$wimg,
            'cptype'=>$cptype,
            'cpname'=>$cpname,
            'cpphone'=>$cpphone,
            'cptime'=>$cptime,
            'cpwork'=>$cpwork,
            'sosname'=>$sosname,
            'cpadd'=>$cpadd,
            'sosmobile'=>$sosmobile,
            'sosname1'=>$sosname1,
            'sosmobile1'=>$sosmobile1,
            'sosname2'=>$sosname2,
            'sosmobile2'=>$sosmobile2,
            'sosname3'=>$sosname3,
            'sosmobile3'=>$sosmobile3,
            'bank'=>$this->request->param('bank'),
            'address'=>$this->request->param('address')
        ];

        foreach ($data as $k=>$v)
            if(empty($v)){
                $this->error('请填写完整信息！'.$k);
            }


        $res=Db::name('daikuan')
            ->where(['id'=>$id])
            ->update($data);


        $this->success('提交成功',['id'=>$id]);

    }

    /**
        详情
     */

    public function getinfobyid(){
        $id=$this->request->param('id');

        $res=Db::name('daikuan')
            ->where(['id'=>$id])
            ->find();

        $this->success('成功',$res);
    }


}